package com.swq.java.code.chapter18.code14;

import com.swq.java.code.utils.PathUtil;

import java.io.*;

public class FileUtil {
    /**
     * 从一个文件中逐行读取字符串，采用本地平台的字符编码
     */
    public void readFile(String fileName) throws IOException {
        readFile(fileName, null);
    }

    /**
     * 从一个文件中逐行读取字符串，参数charsetName指定文件的字符编码
     */
    public void readFile(String fileName, String charsetName) throws IOException {
        InputStream in = new FileInputStream(fileName);
        InputStreamReader reader;
        if (charsetName == null)
            reader = new InputStreamReader(in);
        else
            reader = new InputStreamReader(in, charsetName);
        BufferedReader br = new BufferedReader(reader);
        String data;
        while ((data = br.readLine()) != null)  //逐行读取数据
            System.out.println(data);

        br.close();
    }

    /**
     * 把一个文件中的字符内容拷贝到另一个文件中，并且进行了相关的字符编码转换
     */
    public void copyFile(String from, String charsetFrom, String to, String charsetTo) throws IOException {
        InputStream in = new FileInputStream(from);
        InputStreamReader reader;
        if (charsetFrom == null)
            reader = new InputStreamReader(in);
        else
            reader = new InputStreamReader(in, charsetFrom);
        BufferedReader br = new BufferedReader(reader);

        OutputStream out = new FileOutputStream(to);
        OutputStreamWriter writer = new OutputStreamWriter(out, charsetTo);
        BufferedWriter bw = new BufferedWriter(writer);
        PrintWriter pw = new PrintWriter(bw, true);

        String data;
        while ((data = br.readLine()) != null)
            pw.println(data);  //向目标文件逐行写数据

        br.close();
        pw.close();
    }

    public static void main(String args[]) throws IOException {
        final String fileName = PathUtil.getResourcesFilePath("test.txt").toString();
        final String outFileName = PathUtil.getResourcesFilePath("out.txt").toString();
        FileUtil util = new FileUtil();

//按照本地平台的字符编码读取字符
        util.readFile(fileName);

//把test.txt文件中的字符内容拷贝到out.txt中，out.txt采用UTF-8编码
        util.copyFile(fileName, null, outFileName, "UTF-8");

//按照本地平台的字符编码读取字符，读到错误的数据
        util.readFile(fileName);

//按照UTF-8字符编码读取字符
        util.readFile(outFileName, "UTF-8");
    }
}